using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using Entities;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class tbTrangThai_GiaoDichDAL
    {
        private SqlConnection cn = null;
        public tbTrangThai_GiaoDichDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public void Insert(tbTrangThai_GiaoDichEntities TTGD)
        {
            SqlCommand cm = new SqlCommand("sp_tbTrangThai_GiaoDich_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenTT", TTGD.TenTT);
            cm.Parameters.AddWithValue("@MoTa", TTGD.MoTa);
            cm.ExecuteNonQuery();
        }

        public void Update(tbTrangThai_GiaoDichEntities TTGD)
        {
            SqlCommand cm = new SqlCommand("sp_tbTrangThai_GiaoDich_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenTT", TTGD.TenTT);
            cm.Parameters.AddWithValue("@MoTa", TTGD.MoTa);
            cm.ExecuteNonQuery();
        }

        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("sp_tbTrangThai_GiaoDich_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTT", iD);
            cm.ExecuteNonQuery();
        }

        public Entities.tbTrangThai_GiaoDichEntities.tbTrangThai_GiaoDichColection Get(string where, int pageIndex,int pageSize,string orderBy,string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_tbTrangThai_GiaoDich_GET", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection",
            orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction =ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            Entities.tbTrangThai_GiaoDichEntities.tbTrangThai_GiaoDichColection TrangThai_GiaoDichColection = new Entities.tbTrangThai_GiaoDichEntities.tbTrangThai_GiaoDichColection();
            while (dr.Read())
            {
            tbTrangThai_GiaoDichEntities TTGD = new tbTrangThai_GiaoDichEntities();
            TTGD.MaTT = int.Parse(dr.GetValue(0).ToString());
            TTGD.TenTT = dr.GetValue(1).ToString();
            TTGD.MoTa = dr.GetValue(2).ToString();
            TrangThai_GiaoDichColection.Add(TTGD);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return TrangThai_GiaoDichColection;
        }
    }
}
